/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.animation.Animation;
import com.android1.amarena2d.annotations.Incomplete;
import com.android1.amarena2d.annotations.NotThreadsave;
import com.android1.amarena2d.commons.collections.FixedSizeMap;
import com.android1.amarena2d.commons.collections.IntHashMap;
import com.android1.amarena2d.nodes.behavior.Tagged;
import com.android1.amarena2d.texture.TextureAtlas;



/**
 * Put resources here on startup for easy access.
 * Don't access the library to frenquently.
 * It is just for distributing resources during loading phases.
 */
@NotThreadsave
@Incomplete
public class LibrarySystem extends EngineObject {


    IntKeyLibrary<Tagged> tagged;
    StringKeyLibrary<Animation> animations;
    IntKeyLibrary<TextureAtlas> atlas;

    public LibrarySystem() {
        tagged = new IntKeyLibrary<Tagged>(30);
        animations = new StringKeyLibrary<Animation>(30);
        atlas = new IntKeyLibrary<TextureAtlas>(30);
    }

    public StringKeyLibrary<Animation> animationsLib() {
        return animations;
    }

    public IntKeyLibrary taggedLib() {
        return tagged;
    }

    public IntKeyLibrary atlasLib() {
        return atlas;
    }

    public static final class StringKeyLibrary<T> {
        FixedSizeMap<String, T> elements;

        public StringKeyLibrary(int size) {
            elements = new FixedSizeMap<String, T>(size);
        }

        public T find(String key) {
            return elements.get(key);
        }

        public void store(String k, T element) {
            elements.put(k, element);
        }

        public void clear() {
            elements.clear();
        }
    }

    public static final class IntKeyLibrary<T> {
        IntHashMap<T> elements;

        public IntKeyLibrary(int size) {
            elements = new IntHashMap<T>(size);
        }

        public T find(int key) {
            return elements.get(key);
        }

        public void store(int k, T element) {
            elements.put(k, element);
        }

        public void clear() {
            elements.clear();
        }
    }

    public void dispose() {
        tagged.clear();
        animations.clear();
        atlas.clear();

    }


}
